package com.nintendo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nintendo.mapper.CircleMapper;
import com.nintendo.mapper.CommentMapper;
import com.nintendo.mapper.FriendLikedMapper;
import com.nintendo.mapper.UsersTSTMapper;
import com.nintendo.pojo.Comment;
import com.nintendo.pojo.FriendCircle;
import com.nintendo.pojo.FriendCircleLiked;
import com.nintendo.pojo.Users;
import com.nintendo.pojo.bo.FriendCircleBO;
import com.nintendo.pojo.vo.CommentVO;
import com.nintendo.pojo.vo.FriendCircleVO;
import com.nintendo.service.FriendCircleService;
import com.nintendo.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FriendCircleServiceImpl extends ServiceImpl<CircleMapper,FriendCircle> implements FriendCircleService {
    @Autowired
    private CircleMapper circleMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private FriendLikedMapper friendLikedMapper;
    @Autowired
    private UsersTSTMapper usersTSTMapper;
    @Autowired
    private CommentMapper commentsMapper;
    @Override
    public List<FriendCircleVO> findCirclesByMyId(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        List<FriendCircleVO> friendCircles = circleMapper.selectCirclesByMyId(userId);
        List<String> friendCircleIds = friendCircles.stream().map(FriendCircleVO::getFriendCircleId).collect(Collectors.toList());
        List<FriendCircleLiked> friendCircleLikeds  = friendLikedMapper.findFriendCircleLikedByFriendCircleIds(friendCircleIds);
        Map<String, List<FriendCircleLiked>> friendCircleLikedMaps = friendCircleLikeds.stream().collect(Collectors.groupingBy(FriendCircleLiked::getFriendCircleId));
        for (FriendCircleVO friendCircle : friendCircles) {
            friendCircle.setLikedFriends(friendCircleLikedMaps.get(friendCircle.getFriendCircleId()));
            for (FriendCircleLiked friendCircleLiked : friendCircleLikeds) {
                if(friendCircleLiked.getFriendCircleId().equals(friendCircle.getFriendCircleId())&&friendCircleLiked.getLikedUserId().equals(userId)){
                    friendCircle.setDoILike(true);
                }
            }
        }
        List<CommentVO> commentList =  commentsMapper.findCommentsByFriendCircleIds(friendCircleIds);
        Map<String, List<CommentVO>> commentListMap = commentList.stream().collect(Collectors.groupingBy(CommentVO::getFriendCircleId));
        for (FriendCircleVO friendCircle : friendCircles) {
            friendCircle.setCommentList(commentListMap.get(friendCircle.getFriendCircleId()));
        }
        //去重
        return friendCircles.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public void publishCircle(FriendCircleBO friendCircleBO, HttpServletRequest request) {
        String replaceImages = friendCircleBO.getImages()
                .replace("[", "")
                .replace("]", "")
                .replace("\"","");
        friendCircleBO.setImages(replaceImages);
        FriendCircle friendCircle = BeanUtil.copyProperties(friendCircleBO, FriendCircle.class);
        String userId = request.getHeader("userId");
        friendCircle.setUserId(userId);
        friendCircle.setId(String.valueOf(snowFlake.nextId()));
        friendCircle.setPublishTime(LocalDateTime.now());
        circleMapper.insert(friendCircle);
    }
    @Transactional
    @Override
    public void circleLike(String friendCircleId, Boolean doILike, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        Users users = usersTSTMapper.findUserById(userId);
        FriendCircle friendCircle = circleMapper.selectById(friendCircleId);
        String belongUserId = friendCircle.getUserId();
        FriendCircleLiked friendCircleLiked = new FriendCircleLiked();
        friendCircleLiked.setId(String.valueOf(snowFlake.nextId()));
        friendCircleLiked.setBelongUserId(belongUserId);
        friendCircleLiked.setFriendCircleId(friendCircle.getId());
        friendCircleLiked.setCreatedTime(LocalDateTime.now());
        friendCircleLiked.setLikedUserName(users.getNickname());
        friendCircleLiked.setLikedUserId(userId);
        if (doILike){
            friendLikedMapper.insert(friendCircleLiked);
        }else{
            friendLikedMapper.deleteByFriendCircleIdAndUserId(friendCircleId,userId);
        }

    }
}
